package com.abe.libcore.view.common

import android.app.Activity
import android.app.AlertDialog
import android.app.ProgressDialog
import android.view.LayoutInflater
import com.abe.libquick.R
import java.lang.ref.WeakReference

object LoadDialog {
    private var dialog: AlertDialog? = null
    private var mActivity: WeakReference<Activity>? = null

    fun show(activity: Activity?) {
        if (activity == null) return
        mActivity = WeakReference(activity)
        activity.runOnUiThread {
            if (!activity.isFinishing) {
                dialog = ProgressDialog(activity)
                //dialog.setTitle("");
                dialog!!.setMessage("Loading...")
                dialog!!.setCancelable(true)
                if (!dialog!!.isShowing) {
                    dialog!!.show()
                }
            }
        }
    }

    fun showBrief(activity: Activity?) {
        if (activity == null) return
        mActivity = WeakReference(activity)
        activity.runOnUiThread {
            if (!activity.isFinishing) {
                dialog = AlertDialog.Builder(activity, R.style.TransparentDialog).create()
                val view = LayoutInflater.from(activity).inflate(R.layout.progress_dialog_cus, null)
                dialog!!.setView(view)
                dialog!!.setCancelable(true)
                if (!dialog!!.isShowing) {
                    dialog!!.show()
                }
            }
        }
    }

    fun hide(activity: Activity?) {
        var activity = activity
        if (activity == null) {
            if (mActivity == null) {
                return
            }
            activity = mActivity!!.get()
        }
        if (activity == null) return
        activity.runOnUiThread {
            if (dialog != null && dialog!!.isShowing) {
                dialog!!.dismiss()
                dialog = null
            }
        }
    }
}